package cn.uce.cc.rest.control;

import cn.uce.base.PubApiObjectConstants;
import cn.uce.base.ccos.OrderPostParamsVo;
import cn.uce.base.ccos.OrderServiceFeeVo;
import cn.uce.base.ccos.OrderVo;
import cn.uce.cc.api.service.biz.ICcosOrderBiz;
import cn.uce.cc.srv.utils.CreateOrderCodeUtil;
import cn.uce.utils.StringUtil;
import cn.uce.web.common.base.BaseController;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 查询路由网点方法
 */
@Controller
@RequestMapping(value = "/ccos/order")
public class CcosOrderController extends BaseController {

    private Logger log = LoggerFactory.getLogger(CcosOrderController.class);

    @Resource
    private ICcosOrderBiz ccosOrderBiz;


    @RequestMapping(value="/save",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> save(@RequestParam Boolean isOffer, @RequestParam
            String offerMoney, @RequestParam Boolean isCollection, @RequestParam String collectionMoney, @RequestParam String orderVos){
        try {
            OrderVo orderVo= JSONObject.parseObject(orderVos,OrderVo.class);
            OrderPostParamsVo orderPostParamsVo =new OrderPostParamsVo();
            orderVo.setOrderChannel(14);
            List<OrderServiceFeeVo> serviceFeeList=converObject( isOffer,offerMoney,isCollection,collectionMoney);
            if (null!=serviceFeeList && serviceFeeList.size()>0) {
                orderVo.setServiceFeeList(serviceFeeList);
            }
            orderVo.setOutsideNo(CreateOrderCodeUtil.DateUtilCode());
            orderPostParamsVo.setOrderVo(orderVo);

            log.warn("ccosorder,isOffer"+isOffer+",offerMoney"+offerMoney+",isCollection"+isCollection+",collectionMoney"+collectionMoney+",orderVos"+orderVos);
            String isTrue=ccosOrderBiz.saveOrder(orderPostParamsVo);
            log.warn("ccosordereturn"+isTrue);
            JSONObject js = JSONObject.parseObject(isTrue);
            if ("true".equals(String.valueOf(js.get("success")))){
                return returnSuccess(isTrue);
            }else{
                return returnError(isTrue);
            }
        } catch (Exception e) {
            logger.error("保存订单记录信息出现异常："+e);
        }
        return null;
    }


    public List<OrderServiceFeeVo> converObject(@RequestParam Boolean isOffer,@RequestParam
            String offerMoney,@RequestParam Boolean isCollection,@RequestParam String collectionMoney) {
        List<OrderServiceFeeVo> serviceFeeList = new ArrayList<OrderServiceFeeVo>();
        if (isOffer&&!StringUtil.isBlank(offerMoney)) {
            /*保价*/
            OrderServiceFeeVo  osfVo1 = new OrderServiceFeeVo();
            osfVo1.setOutFeeAmt(BigDecimal.valueOf(Long.valueOf(offerMoney)));
            osfVo1.setServiceTypeCode(PubApiObjectConstants.serviceTypeCode_offer);
            serviceFeeList.add(osfVo1);
        }
        /*代收货款*/
        if (isCollection&&!StringUtil.isBlank(collectionMoney)) {
            OrderServiceFeeVo  osfVo2 = new OrderServiceFeeVo();
            osfVo2.setAttribute5(collectionMoney);
            osfVo2.setServiceTypeCode(PubApiObjectConstants.serviceTypeCode_Collection);
            serviceFeeList.add(osfVo2);
        }
        return serviceFeeList;
    }


}
